﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Hxj.Data;

namespace 致远同步
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            GetDbSession();
        }

        private static DbSession GetDbSession()
        {
            DbSession dbSessionDefault = new DbSession(DatabaseType.SqlServer, System.Configuration.ConfigurationManager.AppSettings["YWDB"]);
            return dbSessionDefault;
        }

        private static DbSession GetEFCWZJKSession()
        {
            DbSession dbSessionDefault = new DbSession(DatabaseType.SqlServer, System.Configuration.ConfigurationManager.AppSettings["EFCWZJK"]);
            return dbSessionDefault;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DbSession ywdb = GetDbSession();
            DbSession efdb = GetEFCWZJKSession();
            bool btk = false;
            Hxj.Data.DbTrans trans = ywdb.BeginTransaction();
            try
            {
                //WhereClip w = WhereClip.All;
                //w = w.And(Hxj.Model.YW_yebgjl._.bglx == "5");
                //w = w.And(Hxj.Model.YW_yebgjl._.synchronization.IsNull() || Hxj.Model.YW_yebgjl._.synchronization==0);
                List<Hxj.Model.YW_yebgjl> lyebgjl;
                List<Hxj.Model.YW_yebgjl> lyebgjl_zy;
                #region 同步门诊
                lyebgjl = ywdb.From<Hxj.Model.YW_yebgjl>().Where(Hxj.Model.YW_yebgjl._.bglx == "5" && (Hxj.Model.YW_yebgjl._.synchronization == false || Hxj.Model.YW_yebgjl._.synchronization.IsNull())).ToList();
                lyebgjl_zy = ywdb.From<Hxj.Model.YW_yebgjl>().Where(Hxj.Model.YW_yebgjl._.bglx == "6" && (Hxj.Model.YW_yebgjl._.synchronization == false || Hxj.Model.YW_yebgjl._.synchronization.IsNull())).ToList();
                foreach (Hxj.Model.YW_yebgjl y in lyebgjl)
                    mzsync(ywdb, efdb, trans, y,btk);
                #endregion

                #region 同步住院
               
                foreach (Hxj.Model.YW_yebgjl y in lyebgjl_zy)
                    zysync(ywdb, efdb, trans, y,btk);
                #endregion
                trans.Commit();
            }
            catch (Exception ee)
            {
                trans.Rollback();
                MessageBox.Show(ee.Message.ToString());
            }
            finally
            {
                trans.Close();
            }

        }

        /// <summary>
        /// 门诊同步
        /// </summary>
        /// <param name="ywdb"></param>
        /// <param name="efdb"></param>
        /// <param name="trans"></param>
        /// <param name="y"></param>
        private static void mzsync(DbSession ywdb, DbSession efdb, Hxj.Data.DbTrans trans, Hxj.Model.YW_yebgjl y,Boolean btk)
        {
            int i = efdb.From<Hxj.Model.门诊_交易汇总>().Where(Hxj.Model.门诊_交易汇总._.结算ID == y.xgdjh.Trim()).Count();
            if (i == 1)
            {
                Hxj.Model.门诊_交易汇总 h = efdb.From<Hxj.Model.门诊_交易汇总>().Where(Hxj.Model.门诊_交易汇总._.结算ID == y.xgdjh.Trim()).ToFirst();
                h.yebgjl_id = y.yw_id;
                h.收退费标志 = btk;
                y.Attach();
                y.synchronization = true;
                ywdb.Update<Hxj.Model.YW_yebgjl>(y, trans);
                ywdb.Insert<Hxj.Model.门诊_交易汇总>(h, trans);

                foreach (Hxj.Model.门诊_交易明细 m in efdb.From<Hxj.Model.门诊_交易明细>().Where(Hxj.Model.门诊_交易明细._.结算ID == y.xgdjh.Trim()).ToList())
                {
                    m.yebgjl_id = y.yw_id;
                    m.收退费标志 = btk;
                    ywdb.Insert<Hxj.Model.门诊_交易明细>(m, trans);
                }

                foreach (Hxj.Model.门诊_交易支付 z in efdb.From<Hxj.Model.门诊_交易支付>().Where(Hxj.Model.门诊_交易支付._.结算ID == y.xgdjh.Trim()).ToList())
                {
                    z.yebgjl_id = y.yw_id;
                    z.收退费标志 = btk;
                    ywdb.Insert<Hxj.Model.门诊_交易支付>(z, trans);
                }
            }
            else if (i > 1)
            {
                throw new Exception("你玛主键也能一样，你的个去");
            }
        }

        /// <summary>
        /// 住院同步
        /// </summary>
        /// <param name="ywdb"></param>
        /// <param name="efdb"></param>
        /// <param name="trans"></param>
        /// <param name="y"></param>
        private static void zysync(DbSession ywdb, DbSession efdb, Hxj.Data.DbTrans trans, Hxj.Model.YW_yebgjl y,bool btk)
        {
            int i = efdb.From<Hxj.Model.住院_交易汇总>().Where(Hxj.Model.住院_交易汇总._.结算ID == y.xgdjh.Trim()).Count();
            if (i == 1)
            {
                Hxj.Model.住院_交易汇总 h = efdb.From<Hxj.Model.住院_交易汇总>().Where(Hxj.Model.住院_交易汇总._.结算ID == y.xgdjh.Trim()).ToFirst();
                h.yebgjl_id = y.yw_id;
                h.收退费标志 = btk;
                y.Attach();
                y.synchronization = true;
                ywdb.Update<Hxj.Model.YW_yebgjl>(y, trans);
                ywdb.Insert<Hxj.Model.住院_交易汇总>(h, trans);

                foreach (Hxj.Model.住院_交易明细 m in efdb.From<Hxj.Model.住院_交易明细>().Where(Hxj.Model.住院_交易明细._.结算ID == y.xgdjh.Trim()).ToList())
                {
                    m.yebgjl_id = y.yw_id;
                    m.收退费标志 = btk;
                    ywdb.Insert<Hxj.Model.住院_交易明细>(m, trans);
                }

                foreach (Hxj.Model.住院_交易支付 z in efdb.From<Hxj.Model.住院_交易支付>().Where(Hxj.Model.住院_交易支付._.结算ID == y.xgdjh.Trim()).ToList())
                {
                    z.yebgjl_id = y.yw_id;
                    z.收退费标志 = btk;
                    ywdb.Insert<Hxj.Model.住院_交易支付>(z, trans);
                }
            }
            else if (i > 1)
            {
                throw new Exception("你玛主键也能一样，你的个去");
            }
        }

    }
}
